/*
 * 文件名：CostCollectController.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：ZhangWenLiang
 * 修改时间：2020年3月18日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgr.bpast.web;


import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import top.infopub.generic.GenericController;
import top.infopub.mgr.bpast.service.CostCollectService;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.hr.service.OrgInfoService;
import top.infopub.model.bpast.CostCollect;
import top.infopub.model.hr.OrgInfo;
import top.infopub.security.util.SessionUserUtils;

import com.github.pagehelper.Page;
import com.google.common.collect.Maps;


/**
 * 商务成本统计表
 * @author ZhangWenLiang
 * @version 2020年3月18日
 * @see CostCollectController
 * @since
 */
@Controller
@RequestMapping("/costCollect")
public class CostCollectController extends GenericController {

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private CostCollectService costCollectService;

    /**
     * 列表页
     * @return
     */
    @RequestMapping("/list")
    public String list(HttpServletRequest request) {
        String visibleOrgIds = (String)SessionUserUtils.getSessionAttribute("visibleOrgIds");
        List<OrgInfo> orgInfos = new ArrayList<OrgInfo>();
        if (StringUtils.isEmpty(visibleOrgIds)) {
            Map<String, Object> where = Maps.newHashMap();
            where.put("state", "1");
            where.put("orderCondition", " types asc ");
            orgInfos = orgInfoService.selectByMapWhere(where);
        }
        else {
            orgInfos = orgInfoService.selectByVisibleIds(visibleOrgIds);
        }
        request.setAttribute("orgInfos", orgInfos);
        return "bpast/lay-costCollect-list";
    }

    /**
     * 根据条件获取获取列表数据
     * @param stsMonthStart 开始月份
     * @param stsMonthEnd 结束月份
     * @param orgTypeAndId ${org.id}-${org.types}
     * @param orgProName 项目名称
     * @param paramPage
     * @return 
     * @throws ParseException 
     * @see
     */
    @RequestMapping("/queryDataByCondition")
    public @ResponseBody RspVo<CostCollect> queryDataByCondition(String stsMonthStart,String stsMonthEnd,
                                                                     String orgTypeAndId,String orgProName,ParamPage paramPage)
        throws ParseException {
        if (StringUtils.isBlank(stsMonthStart) && StringUtils.isBlank(stsMonthEnd)) {
            return RspVo.getFailureResponse(-1, "请选择统计月份~");
        }
        if (StringUtils.isBlank(orgTypeAndId)) {
            return RspVo.getFailureResponse(-1, "请选择公司主体~");
        }
        Page<CostCollect> page = new Page<CostCollect>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        Date monthStart = DateUtils.parseDate(stsMonthStart, "yyyy年MM月");
        Date monthEnd = DateUtils.parseDate(stsMonthEnd, "yyyy年MM月");
        String orgId = orgTypeAndId.split("-")[0];
        String type = orgTypeAndId.split("-")[1];
        page = costCollectService.selectDataByWhere(monthStart, monthEnd, type, orgId, orgProName, page);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }
}
